import router, { constantRoutes, asyncRouter } from '@/router'

const state = {
  routes: constantRoutes
}

const mutations = {
  // 静态路由 + 根据权限动态路由
  setRouter(state, newRoutes) {
    state.routes = [...constantRoutes, ...newRoutes]
  }
}

// 先拿到需要判断的路由数组
// 传入menu
// 遍历判断的路由数组
// 看遍历的那个路由对象的code是否在传入的menu数组里面
// 筛选出来的数组就是该登录用户可访问的路由功能
const actions = {
  getRealRoutes(context, menu) {
    const realRouter = asyncRouter.filter(item => menu.includes(item.code))
    context.commit('setRouter', [...realRouter])
    router.addRoutes([...realRouter, { path: '*', redirect: '/404', hidden: true }])
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
